<template>
	<el-main class="bg">
		<el-form label-position="right" :model="query" class="form p_4" label-width="120">
			<el-row>
				<el-col :xs="24" :sm="24" :lg="8">
					<el-form-item label="赛事名称">
						<el-input v-model="query.event_name"></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :lg="8">
					<el-form-item label="得分">
						<el-input v-model="query.score"></el-input>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="24" :lg="8">
					<el-form-item label="用户姓名">
						<el-select v-model="query.user_name">
							<el-option v-for="o in list_user_name" :key="o['username']" :label="o['nickname'] + '-' + o['username']"
									   :value="o['user_id']">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :xs="24" :sm="10" :lg="8">
					<el-form-item>
						<el-button type="primary" @click="search()">查询</el-button>
						<el-button @click="reset()" style="margin-right: 74px;">重置</el-button>
						<router-link v-if="user_group == '管理员' || $check_action('/individual_competition_score/table','add') || $check_action('/individual_competition_score/view','add')" class="el-button el-button--default el-button--primary" to="./view?">添加
						</router-link>
						<el-button v-if="user_group == '管理员' || $check_action('/individual_competition_score/table','del') || $check_action('/individual_competition_score/view','del')" class="" type="danger" @click="delInfo()">删除</el-button>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<el-table :data="list" @selection-change="selectionChange" @sort-change="$sortChange" style="width: 100%" id="dataTable">
			<el-table-column fixed type="selection" tooltip-effect="dark" width="55">
			</el-table-column>
			<el-table-column prop="event_name" label="赛事名称"
				v-if="user_group == '管理员' || $check_field('get','event_name')" min-width="200">
			</el-table-column>
			<el-table-column prop="score" label="得分"
				v-if="user_group == '管理员' || $check_field('get','score')" min-width="200">
			</el-table-column>
			<el-table-column prop="judge_comment" label="评委评语"
				v-if="user_group == '管理员' || $check_field('get','judge_comment')" min-width="200">
			</el-table-column>
			<el-table-column prop="user_name" label="用户姓名"
				v-if="user_group == '管理员' || $check_field('get','user_name')" min-width="200">
				<template slot-scope="scope">
					{{ get_user_user_name(scope.row['user_name']) }}
				</template>
			</el-table-column>
			<el-table-column sortable prop="create_time" label="创建时间" min-width="200">
                <template slot-scope="scope">
                	{{ $toTime(scope.row["create_time"],"yyyy-MM-dd hh:mm:ss") }}
                </template>
            </el-table-column>
			<el-table-column sortable prop="update_time" label="更新时间" min-width="200">
                <template slot-scope="scope">
                	{{ $toTime(scope.row["update_time"],"yyyy-MM-dd hh:mm:ss") }}
                </template>
			</el-table-column>
			<el-table-column fixed="right" label="操作" min-width="120" v-if="user_group == '管理员' || $check_action('/individual_competition_score/table','set') || $check_action('/individual_competition_score/view','set') || $check_action('/individual_competition_score/view','get')">
				<template slot-scope="scope">
					<router-link class="el-button el-button--small is-plain el-button--success" style="margin: 5px !important;"
					v-if="user_group == '管理员' || $check_action('/individual_competition_score/table','set') || $check_action('/individual_competition_score/view','set') || $check_action('/individual_competition_score/view','get')"
						:to="'./view?' + field + '=' + scope.row[field]" size="small">
						<span>详情</span>
					</router-link>
				</template>
			</el-table-column>
		</el-table>
		<div class="mt text_center">
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="query.page" :page-sizes="[7, 10, 30, 100]" :page-size="query.size"
				layout="total, sizes, prev, pager, next, jumper" :total="count">
			</el-pagination>
		</div>
	</el-main>
</template>
<script>
	import mixin from "@/mixins/page.js";

	export default {
		mixins: [mixin],
		data() {
			return {
				url_get_list: "~/api/individual_competition_score/get_list?like=0",
				url_del: "~/api/individual_competition_score/del?",
				field: "individual_score_id",
				query: {
					"size": 7,
					"page": 1,
					"event_name": "",
					"score": "",
					"user_name": "",
					"orderby": `create_time desc`
				},
				list: [],
				list_user_name: [],
			}
		},
		methods: {
			get_list_before(param){
				var user_group = this.user.user_group;
				if(user_group != "管理员"){
					switch(user_group){
						case "普通用户":
						param["user_name"] = this.user.user_id;
						break;
					}
				}
				return param;
			},
			/**
			 * 获取用户姓名列表
			 */
			async get_list_user_name() {
				var json = await this.$get("~/api/user/get_list?user_group=普通用户");
				if(json.result && json.result.list){
					this.list_user_name = json.result.list;
				}
				else if(json.error){
					console.error(json.error);
				}
			},
			get_user_user_name(id){
				var obj = this.list_user_name.getObj({"user_id":id});
				var ret = "";
				if(obj){
					ret = obj.nickname+"-"+obj.username;
				}
				return ret;
			},
		},
		created() {
			this.get_list_user_name();
		}
	}
</script>
<style>
	.bg {
		background: white;
	}
	.form.p_4 {
		padding: 1rem;
	}
	.mt {
		margin-top: 1rem;
	}
	.text_center {
		text-align: center;
	}
</style>